package com.billy.ohos.loadingstatusview;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.wrapability.SpecialAbility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.StackLayout;

/**
 * base ability
 *
 * @author billy.qi
 * @since 19/3/19 21:15
 */
public abstract class BaseAbility extends Ability {

    protected StackLayout contentLayout = new StackLayout(this);
    protected Gloading.Holder mHolder;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        StackLayout.LayoutConfig config = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_PARENT);
        contentLayout.setLayoutConfig(config);
    }

    /**
     * make a Gloading.Holder wrap with current activity by default
     * override this method in subclass to do special initialization
     *
     * @see SpecialAbility
     */
    protected void initLoadingStatusViewIfNeed() {
        if (mHolder == null) {
            mHolder = Gloading.getDefault().wrap(this, contentLayout).withRetry(new Runnable() {
                @Override
                public void run() {
                    onLoadRetry();
                }
            });
        }
    }

    protected void onLoadRetry() {

    }

    public void showLoading() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoading();
    }

    public void showLoadSuccess() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadSuccess();
    }

    public void showLoadFailed() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadFailed();
    }

    public void showEmpty() {
        initLoadingStatusViewIfNeed();
        mHolder.showEmpty();
    }

}
